using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using PoserLoot.Web.Models;
using System.Web.Security;

namespace PoserLoot.Web.Controllers
{
    public class CommentController : BaseController
    {
        [Authorize(Roles = "Users")]
        public ActionResult Create(Guid id)
        {
            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            return View();
        }

        [Authorize(Roles = "Users")]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Guid id, FormCollection collection)
        {
            Comment comment = new Comment()
            {
                UserId = (Guid)Membership.GetUser().ProviderUserKey,
                Content = collection["Content"],
                Entry = _service.GetEntry(id)
            };

            if (_service.CreateComment(comment))
                return RedirectToAction("Details", "Entry", new { id = id });

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            return View();
        }

        [Authorize(Roles = "Users")]
        public ActionResult Edit(Guid id)
        {
            var comment = _service.GetComment(id);

            if (comment.UserId != (Guid)Membership.GetUser().ProviderUserKey)
                throw new System.Security.SecurityException();

            if (!comment.EntryReference.IsLoaded)
                comment.EntryReference.Load();
            Guid entryId = comment.Entry.EntryId;

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            ViewData["EntryId"] = entryId;
            return View(comment);
        }

        [Authorize(Roles = "Users")]
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            Comment comment = _service.GetComment(id);

            if (comment.UserId != (Guid)Membership.GetUser().ProviderUserKey)
                throw new System.Security.SecurityException();

            comment.Content = collection["Content"];

            if (!comment.EntryReference.IsLoaded)
                comment.EntryReference.Load();
            Guid entryId = comment.Entry.EntryId;

            if (_service.EditComment(comment))
                return RedirectToAction("Details", "Entry", new { id = entryId });

            ViewData["IEnumerable<Category>"] = _service.ListCategories();
            ViewData["EntryId"] = entryId;
            return View();
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(Guid id)
        {
            Comment comment = _service.GetComment(id);

            if (!comment.EntryReference.IsLoaded)
                comment.EntryReference.Load();
            Guid entryId = comment.Entry.EntryId;

            _service.DeleteComment(id);
            return RedirectToAction("Details", "Entry", new { id = entryId });
        }
    }
}
